//
//  SectionHeaderView.m
//  Finance
//
//  Created by cheng xiang on 13-7-21.
//  Copyright (c) 2013年 cheng xiang. All rights reserved.
//

#import "SectionHeaderView.h"

@implementation SectionHeaderView

@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        UIImage *searchImage = [UIImage imageNamed:@"searchbar.png"];
//        [self setBackgroundColor:[UIColor colorWithPatternImage:searchImage]];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(-2, 0, 80, 40);
        btn.tag = 0;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 55, 30)];
        titleLabel.text = @"预期收益";
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.userInteractionEnabled = NO;
        [btn addSubview:titleLabel];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(62, 13, 17, 15)];
        UIImage *backImage = [UIImage imageNamed:@"desc.png"];
        imageView.image = backImage;
        imageView.userInteractionEnabled = NO;
        [btn addSubview:imageView];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(78, 0, 80, 40);
        btn.tag = 0;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 55, 30)];
        titleLabel.text = @"委托期限";
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.userInteractionEnabled = NO;
        [btn addSubview:titleLabel];
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(62, 13, 17, 15)];
        imageView.image = backImage;
        imageView.userInteractionEnabled = NO;
        [btn addSubview:imageView];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(158, 0, 80, 40);
        btn.tag = 0;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 55, 30)];
        titleLabel.text = @"收益类型";
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.userInteractionEnabled = NO;
        [btn addSubview:titleLabel];
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(62, 13, 17, 15)];
        imageView.image = backImage;
        imageView.userInteractionEnabled = NO;
        [btn addSubview:imageView];
        
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(238, 0, 80, 40);
        btn.tag = 0;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 55, 30)];
        titleLabel.text = @"起售金额";
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.userInteractionEnabled = NO;
        [btn addSubview:titleLabel];
        
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(62, 13, 17, 15)];
        imageView.image = backImage;
        imageView.userInteractionEnabled = NO;
        [btn addSubview:imageView];
        
        UIColor* bk = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"top-middle.png"]];
        [self setBackgroundColor:bk];
    }
    return self;
}

- (void)clickBtn:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    for (int i = 0; i < btn.subviews.count; i++) {
        UIView *subview = [btn.subviews objectAtIndex:i];
        if ([subview isKindOfClass:[UIImageView class]]) {
            if (btn.tag == 0) {
                btn.tag = 1;
                UIImage *asImage = [UIImage imageNamed:@"as.png"];
                [(UIImageView *)subview setImage:asImage];
                if ([_delegate respondsToSelector:@selector(clickOnHeader:sort:)]) {
                    NSString *btnTitle = [self getTitleOfButton:btn];
                    [_delegate clickOnHeader:btnTitle sort:@"desc"];
                }
            }else{
                UIImage *backImage = [UIImage imageNamed:@"desc.png"];
                btn.tag = 0;
                [(UIImageView *)subview setImage:backImage];
                if ([_delegate respondsToSelector:@selector(clickOnHeader:sort:)]) {
                    NSString *btnTitle = [self getTitleOfButton:btn];
                    [_delegate clickOnHeader:btnTitle sort:@"incs"];
                }
            }
            break;
        }
    }
}

- (NSString *)getTitleOfButton: (UIButton *)btn
{
    NSString *title = @"";
    for (int i = 0; i < btn.subviews.count; i++) {
        UIView *subview = [btn.subviews objectAtIndex:i];
        if ([subview isKindOfClass:[UILabel class]]) {
            title = [(UILabel *)subview text];
            break;
        }
    }
    return title;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
